package com.xx.springboot.designPattern.ObserverMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoxiunique on 2018-11-13.
 */
public class WeatherData implements Subject {
    private List<Observer> arrayList;

    // 温度
    private float temperature;

    // 湿度
    private float humidity;

    // 压力
    private float pressure;

    public WeatherData() {
        this.arrayList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer o) {
        arrayList.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int index = arrayList.indexOf(o);
        if(index >= 0) {
            arrayList.remove(o);
        }
    }

    @Override
    public void notifyObserver() {
        arrayList.forEach(e -> {
            e.update(temperature, humidity, pressure);
        });
    }


    public void measurementsChanged() {
        notifyObserver();
    }

    public void setMeasurements (float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
